﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Fierce.Core.Aop;
using Fierce.MvcExtend;
using Fierce.Entity.Basic;
using Fierce.MvcExtend.Controllers;
using Fierce.Utility.Helper;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Fierce.Mvc.Areas.Work.Controllers
{
    [Area("Work")]
    public class AuthorizeController : WorkController
    {
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> Login()
        {
            if (IsAuthenticated)
            {
                await HttpContext.SignOutAsync(SignScheme);
            }

            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        public async Task<string> DoLogin(string username, string password)
        {
            var result = ServiceFactory.BasicUser.SignIn(username, password);

            if (result.Code == ServiceResultCode.Success)
            {
                var user = result.Data as BasicUser;

                await HttpContext.SignInAsync(SignScheme, user.ID, username, user.IsSuper);
            }

            return ServiceResult.Serialize(result.Code, result.Message);
        }
    }
}